import express from "express";
// const cookieParser = require('cookie-parser')
import routes from "./routes";
import apiRoutes from "./services/apiRoutes"
// const renderPage = require("./renderPage")

process.env.API_URL = "http://api.csff.test.trylose.cn"
//const apiUrl = "https://api-csff.cutv.com"
// const apiUrl = "http://api.csff.test.trylose.cn"
// const devProxy = {
//     '^/wapi': {
//         target: process.env.API_URL,
//         changeOrigin: true,
//     },
//     '^/upload/': {
//         target: process.env.API_URL,
//         changeOrigin: true,
//     },
// }

const app = express()

// if (devProxy) {
//     const { createProxyMiddleware } = require('http-proxy-middleware')
//     Object.keys(devProxy).forEach(function (context) {
//         const t = devProxy[context];
//         app.use(context, createProxyMiddleware(t))
//     })
// }

const port = parseInt(process.env.PORT, 10) || 7001

app.use(express.static('dist'));
app.use(express.static('public'));
// app.use(cookieParser());

// app.get('*', async (req, res) => {
//     const { html, redirectUrl } = await renderPage(req);
//     if (!redirectUrl) {
//         res.removeHeader("X-Powered-By");
//         res.removeHeader("ETag");
//         res.send(html);
//     } else {
//         res.redirect(redirectUrl);
//     }
// })
routes.forEach(route => {
    app.get(route.url, async (req, res) => {
        const html = await route.handler(req, res);
        res.send(html);
    });
});
apiRoutes.forEach(route => {
    app.get(route.url, route.handler);
});

app.listen(port, () => console.log(`App listening at http://localhost:${port}`))